package com.brush.web.controller.pig.task;

import com.brush.common.core.controller.BaseController;
import com.brush.common.core.domain.R;
import com.brush.pig.domain.req.task.IotTaskAddReq;
import com.brush.pig.domain.req.task.IotTaskEditReq;
import com.brush.pig.domain.req.task.IotTaskReq;
import com.brush.pig.domain.resp.task.IotTaskResp;
import com.brush.pig.service.IotTaskService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "后台-生猪任务api")
@RestController
@RequestMapping(value = "/admin/pig/task")
public class IotTaskController extends BaseController {

    @Autowired
    private IotTaskService iotTaskService;

    @ApiOperation(value = "查看任务信息")
    @GetMapping("/list")
    public R<PageInfo<IotTaskResp>> pagination(IotTaskReq req){
        PageInfo<IotTaskResp> pageInfo = this.iotTaskService.pagination(req);
        return R.ok(pageInfo);
    }

    @ApiOperation(value = "新增任务")
    @PostMapping
    public R add(@RequestBody @Valid IotTaskAddReq req){
        boolean result = this.iotTaskService.add(req);
        return result ? R.ok() : R.fail();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<IotTaskResp> findById(@PathVariable(value = "id") Long id){
        IotTaskResp iotTaskResp = this.iotTaskService.findById(id);
        return R.ok(iotTaskResp);
    }

    @ApiOperation(value = "修改任务")
    @PutMapping
    public R edit(@RequestBody @Valid IotTaskEditReq req){
        boolean result = this.iotTaskService.edit(req);
        return super.isSuccess(result);
    }

    @ApiOperation(value = "删除任务")
    @DeleteMapping(value = "/{ids}")
    public R deleteById(@PathVariable(value = "ids") Long[] ids){
        boolean result = this.iotTaskService.deleteById(ids);
        return super.isSuccess(result);
    }

}
